package com.ruoyi.web.controller.tax;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IRegisterUserService;
import com.ruoyi.system.service.ItaxUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.activation.CommandInfo;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("tax")
public class TaxController extends BaseController {

    private String prefix = "system/tax";

    @Autowired
    private IRegisterUserService registerUserService;

    @Autowired
    private ItaxUserService itaxUserService;

    /**
     *
     */
    public List<CompanyInfo> selectUserList(CompanyInfo companyInfo){
        return registerUserService.selectUserList(companyInfo);
    }


    /**
     *查询所有注册用户
     */
    @PostMapping("/selectAllRegisterUser")
    @ResponseBody
    public TableDataInfo getAllRegisterUser(CompanyInfo companyInfo){
        startPage();
        List<CompanyInfo> list = registerUserService.selectUserList(companyInfo);

        return getDataTable(list);
    }


    /**
     *通过id查询用户
     */
    @GetMapping("/selectRegisterUserByID/{userid}")
    @ResponseBody
    public CompanyInfo getRegisterUserByID(@PathVariable("userid") long userid){
        return registerUserService.selectRegisterUserByID(userid);
    }

    /**
     *查询所有注册用户
     */
    @GetMapping("/selectAllRegisterUserCount")
    @ResponseBody
    public int getAllRegisterUserCount(){

        return registerUserService.selectAllRegisterUserCount();
    }

    /**
     * 查询今日新增用户
     */
    @GetMapping("/selectRegisterUserByDay")
    @ResponseBody
    public List<InstitutionInfo> getRegisterByDay(){

        return registerUserService.selectRegisterUserByDay();
    }

    /**
     *查询本周新增注册用户
     */
    @GetMapping("/selectRegisterUserByWeek")
    @ResponseBody
    public List<InstitutionInfo> getRegisterByWeek(){
        return registerUserService.selectRegisterUserByWeek();
    }

    /**
     *查询七日新增用户
     */
    @GetMapping("/getSevenDaysUsers")
    @ResponseBody
    public List<RegisterInfo> getSevenDaysUsers(){
        List<InstitutionInfo> institutionInfos = registerUserService.selectSevenDaysUsers();
        List<RegisterInfo> registerInfos = new ArrayList<RegisterInfo>();
        for (int i = 0; i < 7; i++) {
            LocalDate localDate = LocalDate.now();
            RegisterInfo registerInfo = new RegisterInfo();
            registerInfo.setRegisterData(""+localDate.plusDays(-i));
            registerInfos.add(registerInfo);
        }
        for (int i = 0; i < registerInfos.size(); i++) {
            int registerCount = 0;
            for (int j = 0; j < institutionInfos.size(); j++) {
                if (institutionInfos.get(j).getCreatetime().toString().indexOf(registerInfos.get(i).getRegisterData())!=-1) {
                    registerCount ++;
                }
            }
            registerInfos.get(i).setRegisterCount(registerCount);
        }
        Collections.reverse(registerInfos);
        return  registerInfos;
    }


    /**
     *查询30日的新增用户
     */
    @GetMapping("/getThirtyDaysUsers")
    @ResponseBody
    public List<RegisterInfo> getThirtyDaysUsers(){
        List<InstitutionInfo> institutionInfos = registerUserService.selectThirtyDaysUsers();
        List<RegisterInfo> registerInfos = new ArrayList<RegisterInfo>();
        for (int i = 0; i < 30; i++) {
            LocalDate localDate = LocalDate.now();
            RegisterInfo registerInfo = new RegisterInfo();
            registerInfo.setRegisterData(""+localDate.plusDays(-i));
            registerInfos.add(registerInfo);
        }
        for (int i = 0; i < registerInfos.size(); i++) {
            int registerCount = 0;
            for (int j = 0; j < institutionInfos.size(); j++) {
                if (institutionInfos.get(j).getCreatetime().toString().indexOf(registerInfos.get(i).getRegisterData())!=-1) {
                    registerCount ++;
                }
            }
            registerInfos.get(i).setRegisterCount(registerCount);
        }
        Collections.reverse(registerInfos);
        return  registerInfos;
    }
    /**
     *查询当日新增用户数，通过24小时内每2个小时进行分段展示。
     */
    @GetMapping("/getOneDaysUsers")
    @ResponseBody
    public List<RegisterInfo> getOneDaysUsers(){
        List<InstitutionInfo> institutionInfos = registerUserService.selectRegisterUserByDay();
        List<RegisterInfo> registerInfos = new ArrayList<RegisterInfo>();
        LocalTime time = LocalTime.of(00, 00, 00);
        for (int i = 0; i < 12; i++) {
            RegisterInfo registerInfo = new RegisterInfo();
            registerInfo.setRegisterData(""+time);
            registerInfos.add(registerInfo);
            time = time.plus(2, ChronoUnit.HOURS);
        }
        RegisterInfo registerInfo = new RegisterInfo();
        registerInfo.setRegisterData(""+"24:00");
        registerInfo.setRegisterCount(0);
        registerInfos.add(registerInfo);
        for (int i = 0; i < registerInfos.size(); i++) {
            int registerCount = 0;
            for (int j = 0; j < institutionInfos.size(); j++) {
                int countIndex = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.HH,DateUtils.parseDate(institutionInfos.get(j).getCreatetime())))/2;
                int registCount = registerInfos.get(countIndex).getRegisterCount();
                registerInfos.get(countIndex).setRegisterCount(registCount+1);
            }
            return registerInfos;
        }
        return  registerInfos;
    }



    /**
     *更新信息
     */
    @PostMapping("/update")
    @ResponseBody
    public int update(CompanyInfo companyInfo){

        System.out.println(companyInfo );
        if (companyInfo.getUserid()!=null) {
            UserInfo userInfo = new UserInfo();
            userInfo.setAutoid(companyInfo.getUserid());
            userInfo.setEffective(Byte.parseByte(companyInfo.getUserstatus()));
            if (itaxUserService.getUserNameCount(companyInfo.getUsername()) >0) {
                return  -2;
            }else {
                userInfo.setUsername(companyInfo.getUsername());
                return itaxUserService.updateByPrimaryKeySelective(userInfo);
            }

        }else if (companyInfo.getInstitutionid()!=null) {
            InstitutionInfo institutionInfo = new InstitutionInfo();
            institutionInfo.setAutoid(companyInfo.getInstitutionid());
            institutionInfo.setName(companyInfo.getCompname());
            institutionInfo.setCode(companyInfo.getSocialcreditcode());
            institutionInfo.setIndustrycode(companyInfo.getIndustrycode4());
            institutionInfo.setIndustryname(companyInfo.getIndustryname4());
            institutionInfo.setProvince(companyInfo.getProvince());
            institutionInfo.setCity(companyInfo.getCity());
            institutionInfo.setDistrict(companyInfo.getDistrict());
            institutionInfo.setRegisplace(companyInfo.getProvince().concat(companyInfo.getCity().concat(companyInfo.getDistrict())));
            return registerUserService.updateByPrimaryKeySelective(institutionInfo);
        }
        return -1;
    }


    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyInfo companyInfo)
    {
        List<CompanyInfo> list = registerUserService.selectUserList(companyInfo);
        ExcelUtil<CompanyInfo> util = new ExcelUtil<CompanyInfo>(CompanyInfo.class);
        return util.exportExcel(list, "字典类型");
    }

    @PostMapping("/search")
    @ResponseBody
    public List<CompanyInfo> getSearResult(CompanyInfo companyInfo)
    {
       return registerUserService.getSearchResult(companyInfo);
    }


    /**
     *查询本月新增注册用户
     */
    @GetMapping("/selectRegisterUserByMonth")
    @ResponseBody
    public List<InstitutionInfo> getRegisterByMonth(){

        return registerUserService.selectRegisterUserByMonth();
    }

    /**
     *查询本季度新增注册用户
     */
    @GetMapping("/selectRegisterUserByQuarter")
    @ResponseBody
    public List<InstitutionInfo> getRegisterByQuarter(){

        return registerUserService.selectRegisterUserByQuarter();
    }

    /**
     *查询本月新增注册用户
     */
    @GetMapping("/selectRegisterUserByYear")
    @ResponseBody
    public List<InstitutionInfo> getRegisterByYear(){

        return registerUserService.selectRegisterUserByYear();
    }

    @RequestMapping("/main")
    public String main(ModelMap map) {
        map.addAttribute("totalCount", getAllRegisterUserCount());
        map.addAttribute("todayCount", getRegisterByDay().size());
        map.addAttribute("weekCount", getRegisterByWeek().size());

        return prefix+"/main";
    }




    @RequestMapping("/taxUser")
    public String taxUser(ModelMap map) {
        map.put("companyList",registerUserService.selectAllRegisterCompanyList());
        return prefix+"/taxUser";
    }


    @RequestMapping("/userRole")
    public String userRole(ModelMap map) {

        return prefix+"/userRole";
    }

    @RequestMapping("/userDetail/{insititutionId}")
    public String userDetail(@PathVariable("insititutionId") int insititutionId,  ModelMap mmap) {

        mmap.put("companyInfoModel", registerUserService.selectRegisterUserByID(insititutionId));

        return prefix+"/userDetail";
    }
}

